var app = getApp();
var Bmob = require('../../utils/bmob.js');
var secondNum = 60;
Page({
  data: {
    getSmsCodeDisabled:false,
    smsCodeBtnText:'点击获取',
    phoneNumber:'',
    smsCode:''
  },
  onLoad: function () {
    app.userBmobLogin();
  },
  phoneNumberInput:function(e){
    this.data.phoneNumber = e.detail.value;
  },
  smsCodeInput:function(e){
    this.data.smsCode = e.detail.value;
  },
  applySubmit: function (e) { //提交验证
    var self = this;
    if (app.isNullEmptyOrUndefined(self.data.phoneNumber)) {
      wx.showModal({
        title: '',
        content: '请填写手机号码！',
        showCancel: false
      });
      return;
    }
    if (app.isNullEmptyOrUndefined(self.data.smsCode)){
      wx.showModal({
        title: '',
        content: '请填写短信验证码！',
        showCancel: false
      });
      return;
    }
    Bmob.verifySmsCode({ 'mobilePhoneNumber': self.data.phoneNumber,'smsCode': self.data.smsCode}).then(function (response) {
      console.log(response);
      if (response.error){
        wx.showModal({
          title: '错误',
          content: response.error,
          showCancel: false,
        });
        return;
      }
      wx.showLoading({
        title: '请稍后...',
      });
      var userQuery = Bmob.Query('_User');
      if (Bmob.User.current() == null) {
        console.log("未登录");
      }
      else {
        userQuery.set('id', Bmob.User.current().objectId);
        userQuery.set("mobilePhoneNumber", self.data.phoneNumber);
        userQuery.save().then(result => {
          app.globalData.mobilePhoneNumber = self.data.phoneNumber;
          console.log(result);
          wx.showModal({
            title: '成功',
            content: '恭喜，绑定成功！',
            showCancel: false,
            complete: function () {
              wx.navigateBack();
            }
          });
          wx.hideLoading();

        }).catch(error => {
          console.log(error);
          wx.showModal({
            title: '错误',
            content: '手机号格式错误！',
            showCancel: false
          });
          wx.hideLoading();
        });
      }


    }).catch(function (error) {
      console.log(error);
      wx.showModal({
        title: '错误',
        content: error,
        showCancel: false
      });
    });
  },
  getSmsCode: function (e) { //获取短信验证码，手机号绑定
    var self = this;
    var smsId = "";

    //开始验证短信
    Bmob.requestSmsCode({ "mobilePhoneNumber": self.data.phoneNumber, "template": "默认验证码" })
      .then(function (response) {
        console.log(response);
        smsId = response.smsId;
        //设置禁用
        self.setData({
          getSmsCodeDisabled: !self.data.getSmsCodeDisabled
        });
        var secStr = setInterval(function () {
          self.setData({
            smsCodeBtnText: '点击获取' + secondNum + 's'
          });
          secondNum--;
          if (secondNum == 0) {
            clearInterval(secStr);
            self.setData({
              getSmsCodeDisabled: false,
              smsCodeBtnText: '点击获取'
            });
          }
        }, 1000);
      })
      .catch(function (error) {
        console.log(error);
      });

    
  }
})
